/**
 * 项目树形菜单
 * @param {} config
 */
DC.front.ExplorerPanel = function(config) {
	var treePanel = this;
	var url = DC.url.GET_PROJECT_LIST;
	
	DC.front.ExplorerPanel.superclass.constructor.call(this, Ext.apply({
		id : 'explorer_panel',
        title : '<div style="float:left;">My Projects</div><div style="float:right;"><a href="javascript:void(0);" id="explorer_panel_collapse_all"><img src="././image/ico_collapse.gif" alt="Collapse All" /></a><a href="javascript:void(0);" id="explorer_panel_expand_all"><img src="././image/ico_expand.gif"  alt="Expand All" /></a></div>',
		margins : '3 0 3 3',
		cmargins : '0 0 0 0',
		width : 200,
		region : 'west',
		split : true,
		useArrows : true,
		animate : true,
        enableDD : true,
		collapsible : true,
        autoScroll : true,
        rootVisible : false,
		root : new Ext.tree.AsyncTreeNode({
			id : 'myprojects',
			text : 'App',
			draggable: false
		}),
		loader : new Ext.tree.TreeLoader({
			preloadChildren : false, // 在第一次加载子节点后是否递归加载所有子节点。
			clearOnLoad : false, // 在加载前是否移除已存在的子节点，默认为true。
			dataUrl : url
		}),
		listeners : {
            beforeload : function(node) {
			   if (node.getDepth() == 0) {
			   		treePanel.loader.dataUrl = url;
                    treePanel.loader.baseParams = {
						depth : 0
				    };
                } else if (node.getDepth() == 1) {
                    treePanel.loader.dataUrl = url;
					treePanel.loader.baseParams = {
						depth : 1,
						parentPath : node.attributes.text
				    };
				}
			    //node.getOwnerTree().ownerCt.body.mask('Loading...', 'x-mask-loading');
            },
            load : function(node) {
			    //node.getOwnerTree().ownerCt.body.unmask();
			    new Ext.tree.TreeSorter(treePanel, { // 当加载树时对树进行排序，将叶子节点放在非叶子节点后面
			        folderSort: true
			    });
            },
			contextmenu : function(node, e) {
				//treePanel.treeRightKeyMenu(node, e); 
            },
            click : function(node, e) {
            	Ext.get("status_bar_file_path").addClass('folder');
            	Ext.get("status_bar_file_path").update(treePanel.getFilePath(node));
            	Ext.getCmp('sys_menu_rename').enable();
            	Ext.getCmp("sys_menu_delete").enable();
            	Ext.getCmp('sys_toolbar_delete').enable();
            	Ext.getCmp('sys_menu_download_zipped_file').enable();
            	Ext.getCmp('sys_toolbar_download').enable();
            	if (!node.isLeaf()) {
			    	Ext.getCmp("sys_toolbar_new_file").enable();
			    	Ext.getCmp("sys_toolbar_new_folder").enable();
			    	Ext.getCmp("sys_menu_new_file").enable();
			    	Ext.getCmp("sys_menu_new_folder").enable();
			    	Ext.getCmp('sys_menu_import_file').enable();
			    	Ext.getCmp('sys_menu_download_file').disable();
			    } else {
			    	Ext.getCmp("sys_toolbar_new_file").disable();
			    	Ext.getCmp("sys_toolbar_new_folder").disable();
			    	Ext.getCmp("sys_menu_new_file").disable();
			    	Ext.getCmp("sys_menu_new_folder").disable();
			    	Ext.getCmp("sys_menu_new_class").disable();
			    	Ext.getCmp('sys_menu_import_file').disable();
			    	Ext.getCmp('sys_menu_download_file').enable();
			    }
            	if (node.getDepth() == 1) {
            		Ext.getCmp("sys_menu_close_project").enable();
            		Ext.getCmp("sys_toolbar_build").enable();
            		Ext.getCmp("sys_menu_build").enable();
            		Ext.getCmp("sys_menu_download_and_build_project").enable();
            		Ext.getCmp("sys_menu_new_class").disable();
			    	Ext.getCmp("sys_toolbar_new_class").disable();
			    	Ext.getCmp("sys_menu_run").disable();
            	} else if (node.getDepth() >= 2) {
            		Ext.getCmp("sys_menu_close_project").enable();
            		Ext.getCmp("sys_menu_download_and_build_project").disable();
            		Ext.getCmp("sys_toolbar_build").disable();
            		Ext.getCmp("sys_menu_build").disable();
            		var array = node.getPath().split('/');
            		if (!node.isLeaf()) {
    				    if (treePanel.getNodeById(array[3]).attributes.text == 'src') {
    				    	Ext.getCmp("sys_menu_new_class").enable();
    				    	Ext.getCmp("sys_toolbar_new_class").enable();
    				    	
    				    } else {
    				    	Ext.getCmp("sys_menu_new_class").disable();
    				    	Ext.getCmp("sys_toolbar_new_class").disable();
    				    }
    				    Ext.getCmp("sys_menu_run").disable();
            		} else {
            			if (treePanel.getNodeById(array[3]).attributes.text == 'src') {
            				Ext.getCmp("sys_menu_run").enable();
    				    } else {
    				    	Ext.getCmp("sys_menu_run").disable();
    				    }
            			
            		}
            	}else {
            		Ext.getCmp("sys_menu_close_project").disable();
            		Ext.getCmp("sys_toolbar_build").disable();
            		Ext.getCmp("sys_menu_build").disable();
            		Ext.getCmp("sys_menu_download_and_build_project").disable();
            		Ext.getCmp("sys_menu_new_class").disable();
			    	Ext.getCmp("sys_toolbar_new_class").disable();
			    	Ext.getCmp("sys_menu_run").disable();
            	}
			},
			dblclick : function(node, e) {
				if (node.isLeaf()) {
					this.openFile(node);
				}
			},
            expandnode : function(node) {
            	if (node.getDepth() != 0) {
            		Ext.state.Manager.set("treestate", node.getPath()); //将树节点的状态保存下来
            	}
			},
			collapsenode : function(node) {
				if (node.getDepth() != 0) {
            		Ext.state.Manager.set("treestate", node.getPath()); //将树节点的状态保存下来
            	}
			}
		}
    }, config || {}));
    /*
     * 将树的状态还原为上次保存的样子
     */
    var treeState = Ext.state.Manager.get("treestate");
    if (treeState) {
    	treePanel.expandPath(treeState);
    }
};

Ext.extend(DC.front.ExplorerPanel, Ext.tree.TreePanel, {
	 /** 
     * 重新加载数据 
     * @param params 
     */  
    reloadData : function(node, params) {
        /*var scope = this;  
        var treeLoad = this.getLoader();  
        
        if (node == null)
        	node = this.getRootNode();
        if( params)  
        	treeLoad.baseParams = params;  
          
        treeLoad.load(node, function(){
        	return;
        }, scope);*/
    	this.getRootNode().reload();
    	var treeState = Ext.state.Manager.get("treestate");
	    if (treeState) {
	    	this.expandPath(treeState);
	    }
    },
    
	getSelectedNodePath : function() {
		var path = "";
		var selectedNode = this.getSelectionModel().getSelectedNode();
		if (selectedNode) {
			var array = selectedNode.getPath().split('/');
			for (var i = 2, len = array.length; i < len; i++) {
				path = path + this.getNodeById(array[i]).attributes.text + "/";
			}
		}
		return path;
	},
	
	getFilePath : function(node) {
		var path = "";
		if (node) {
			var array = node.getPath().split('/');
			for (var i = 2, len = array.length; i < len; i++) {
				path = path + this.getNodeById(array[i]).attributes.text + "/";
			}
		}
		return path.substring(0, path.length - 1);
	},
	
	getNodePath : function(node) {
		var path = "";
		if (node) {
			var array = node.getPath().split('/');
			for (var i = 2, len = array.length; i < len - 1; i++) {
				path = path + this.getNodeById(array[i]).attributes.text + "/";
			}
		}
		return path.substring(0, path.length - 1);
	},
	
	openFile : function(node) {
		var parentPath = this.getNodePath(node);
		var fileName = node.attributes.text;
		var name = fileName.substring(0, fileName.lastIndexOf('.')); // 文件名称
		var type = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length); // 文件类型
		if (type.match(DC.front.variable.FILTER_FILE_TYPE)) {
			console.log(fileName);
			return;
		}
		
		DC.util.CommonAction.loadMask("Openning...");
		Ext.Ajax.request({
		    url : DC.url.OPEN_FILE,
		    method : 'POST',
		    params : {
		    	"cfile.filePath" : parentPath + "/" + fileName,
		    	"cfile.fileName" : fileName,
		    	"cfile.fileType" : type
		    },
		    success : function(response, options){
		    	DC.util.CommonAction.unMask();
			    var repText = Ext.util.JSON.decode(response.responseText);
			    if(repText.returnText.success){// 判断请求是否成功
				   var code_editor = new DC.front.CodeEditor({
						parentPath : parentPath,
						fileName : name,
						fileType : type,
						icon : node.attributes.iconCls,
						code : repText.text
					});
					
			    	DC.obj.editor_tabPanel.addTab({
			    		id : parentPath.replace(/\//g, '_') + '_' + name + '_' + type,
			    		panel : code_editor
			    	});
			    } else {
				    Ext.example.msg('Prompt', repText.returnText.msg, 'prompt');
			    }
		    },
		    failure : function(response, options){
		    	DC.util.CommonAction.unMask();
            	Ext.example.msg('Verification Win', response.responseText, 'error');
		    }
	    });
    	
	}
});